package org.example;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;

public class exercise1 {
    /*保存任意对象数据*/
    public static void main(String[] args) throws IllegalAccessException, IOException {
    Student s = new Student("小A",23,'女',167.5,"睡觉");
    Teacher t = new Teacher("波妞",1000);
        saveObject(s);
    }
    public static void saveObject(Object obj) throws IOException, IllegalAccessException {
        //获取字节码文件
        Class clazz = obj.getClass();

        //创建IO流
        BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
        //获取所有的成员变量
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields){
            field.setAccessible(true);
            //获取成员变量的名字
            String name = field.getName();
            //获取变量值
            Object value = field.get(obj);
            System.out.println(name+value);
            //写出数据
            bw.write(name+ "="+value);
            bw.newLine();
        }
        bw.close();
    }


}
